package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.JSONObjectPro;
import com.ruoyi.common.enums.comlbs.ObjectTypeEnum;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.request.vehicle.NumberPlateExistParam;
import com.ruoyi.system.domain.request.realtimevideo.GetDeviceInfoParam;
import com.ruoyi.system.domain.vo.common.ComDvoName;
import com.ruoyi.system.mapper.VehicleManagementMapper;
import com.ruoyi.system.service.IAlarmRulePersonVehicleService;
import com.ruoyi.system.service.IVehicleEquipmentService;
import com.ruoyi.system.service.IVehicleManagementService;
import com.ruoyi.system.utils.ComLbsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 车辆管理Service业务层处理
 * 
 * @author wp
 * @date 2023-08-10
 */
@Service
public class VehicleManagementServiceImpl extends ServiceImpl<VehicleManagementMapper, VehicleManagement> implements IVehicleManagementService {

    @Autowired
    private VehicleManagementMapper vehicleManagementMapper;
    @Autowired
    private IVehicleEquipmentService vehicleEquipmentService;
    @Autowired
    private IAlarmRulePersonVehicleService alarmRulePersonVehicleService;


    @DataScope
    @Override
    public List<VehicleManagement> selectList(VehicleManagement vehicleManagement) {
        return vehicleManagementMapper.list(vehicleManagement);
    }

    @Override
    public VehicleManagement selectOne(Long id) {
        return vehicleManagementMapper.get(id);
    }

    @Override
    public boolean updateVehicle(VehicleManagement vehicleManagement) {
        VehicleManagement oldVehicle = getById(vehicleManagement.getId());
        //已绑定设备的监管对象无法修改所属公司
        int count = vehicleEquipmentService.count(new QueryWrapper<VehicleEquipment>().eq("vehicle_id", vehicleManagement.getId()));
        if (count > 0 && !oldVehicle.getCompanyId().equals(vehicleManagement.getCompanyId())) {
            //删除新增的文件
            ComLbsUtils.exceptionFileHandle(oldVehicle.getVehiclePic(), vehicleManagement.getVehiclePic(), false);
            throw new ServiceException("此监管对象已绑定设备，无法修改所属公司！");
        }
        //图片处理
        ComLbsUtils.fileHandle(oldVehicle.getVehiclePic(), vehicleManagement.getVehiclePic(), false);
        return updateById(vehicleManagement);
    }

    @Override
    public void updateVehicleByApplyId(Long applyId) {
        vehicleManagementMapper.updateVehicleByApplyId(applyId);
    }

    @Transactional
    @Override
    public void deleteVehicle(Long[] ids) {
        for (Long id : ids) {
            int count = vehicleEquipmentService.count(new QueryWrapper<VehicleEquipment>().eq("vehicle_id", id));
            if (count > 0) throw new ServiceException("操作失败，此监管对象正在使用中");
            //删除监管对象的图片
            VehicleManagement object = getById(id);
            if (object == null) throw new ServiceException("监管对象不存在");
            ComLbsUtils.fileHandle(object.getVehiclePic(), new ArrayList<>(), false);
            //删除监管对象-报警规则关系（解绑）
            if (removeById(id)) alarmRulePersonVehicleService.remove(new QueryWrapper<AlarmRulePersonVehicle>().eq("business_id", id));
        }
    }

    @Override
    @DataScope
    public int getExpireVehicleCount(VehicleManagement model) {
        return vehicleManagementMapper.getExpireVehicleCount(model);
    }

    @Override
    public List<VehicleManagement> listByNumberPlate(String numberPlate) {
        return vehicleManagementMapper.listByNumberPlate(new JSONObjectPro().put("numberPlate", numberPlate));
    }

    @Override
    @DataScope
    public List<ComDvoName> listCompanyOrVehicle(GetDeviceInfoParam param) {
        return vehicleManagementMapper.listCompanyOrVehicle(param);
    }

    @Override
    public RealtimePositioning getPositionByDeviceId(String deviceId) {
        return vehicleManagementMapper.getPositionByDeviceId(deviceId);
    }

    @Override
    @DataScope
    public List<VehicleManagement> listMapVehicle(VehicleManagement model) {
        return vehicleManagementMapper.listMapVehicle(model);
    }

    /**
     * 根据设备号获取监管对象id及名称
     * @return
     */
    @Override
    public VehicleManagement getVehicleIdAndNumberPlate(String deviceId) {
        return vehicleManagementMapper.getVehicleIdAndNumberPlate(deviceId);
    }

    /**
     * 监管对象名称重复校验
     * @param param
     */
    public void checkExistNumberPlate(NumberPlateExistParam param){
        // 如果监管类型为人，不做重复校验
        if (param.getObjType().equals(ObjectTypeEnum.PERSON.getCode())) return;
        QueryWrapper<VehicleManagement> queryWrapper = new QueryWrapper<VehicleManagement>()
                .eq("company_id", param.getCompanyId())
                .eq("obj_type", param.getObjType())
                .eq("number_plate", param.getNumberPlate());
        if (param.getObjId() != null) queryWrapper.ne("id", param.getObjId());
        Integer count = vehicleManagementMapper.selectCount(queryWrapper);
        if (count > 0) throw new ServiceException("监管对象已存在");
    }




}
